home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3418 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.0 KB  |  117 lines

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Nested Structures in C - A Question
  5. Date: 28 Jan 1996 21:01:42 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Jan28140142@qcd.lanl.gov>
  8. References: <36400002@peg>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: tmccoy@peg.apc.org's message of 29 Jan 96 01:38 GMT+1000
  13.  
  14. In article <36400002@peg> tmccoy@peg.apc.org writes:
  15. <snip>
  16.    struct outer {
  17.        int var1;
  18.        int var2;
  19.        int var3;
  20.    };
  21.    
  22.    Apparently the compiler will NOT allocate any storage when
  23.    I do this because, according to Kernighan and Ritchie (2nd
  24.    Ed), "a structure declaration that is not followed by a
  25.    list of variables reserves no storage; it merely describes
  26.    a template or the shape of a structure" (Page 128).
  27.    
  28.    This is all well and good.  And when I want to define a new
  29.    structure *within* my old one, I should be able to do:
  30.    
  31.    struct outer {
  32.        int var1;
  33.        int var2;
  34.        int var3;
  35.        struct inner {
  36.            int nested1;
  37.            int nested2;
  38.        };
  39.    };
  40.  
  41. Why do you think so? If you wanted to declare a new int inside your
  42. struct, would it have been sufficient to say `int;' instead of `int
  43. var4;'? The case with structs is exactly the same: `struct inner' is a
  44. type, like `int': not a field or variable name. 
  45.  
  46. The part in {} following the words struct inner actually specifies
  47. what the type struct inner is. (Someone else said that the declaration
  48. is valid because it declares the tag: that is incorrect. Inside a
  49. struct (e.g. struct outer), struct inner {...}; is a syntax error even
  50. though it declares the tag because a non-bitfield struct member
  51. declaration may not omit the declarator, i.e. the field name. In other
  52. words, only bitfields may be unnamed members.)
  53.  
  54. <snip>
  55.    Yet, C won't let me do this!!  It insists that I define my
  56.    nested structure as follows:
  57.    
  58.    struct outer {
  59.        int var1;
  60.        int var2;
  61.        int var3;
  62.        struct inner {
  63.            int nested1;
  64.            int nested2;
  65.        } DUMMY;
  66.    };
  67.    
  68.    and after doing
  69.    
  70.    struct outer instance;
  71.    
  72.    I must refer to the first member of my inner structure by
  73.    doing:
  74.    
  75.    instance.DUMMY.nested1
  76.    
  77.    i.e. I must access the inner members via a variable (called
  78.    "DUMMY") instead of being able to use my structure tag
  79.    (i.e. "inner").  In fact, the *only way* I can access the
  80.    inner members is by defining the DUMMY variable within the
  81.    structure template of "outer".
  82.  
  83. This is because the struct `tag' is a way of specifying the struct
  84. type. The . operator needs a field name, not a type name. Consider
  85. that the following is valid
  86.  
  87. struct outer {
  88.  struct inner { int nested1; int nested2; } DUMMY1,DUMMY2; 
  89.  } instance;
  90.  
  91. Now what should instance.inner.nested1 (currently syntax error) mean?
  92. instance.DUMMY1.nested1 and instance.DUMMY2.nested2 are obvious.
  93.    
  94. <snip>   
  95.    With a nested structure, as with a simple structure, I
  96.    *should* be able to define a pure structure template in
  97.    which no storage is allocated.  Yet, in the definition I am
  98.    forced to use above, strictly speaking the compiler should
  99.    allocate storage for "DUMMY" even though the enclosing
  100.    structure (i.e. "outer") is only a template.
  101.  
  102. No, no storage is allocated. Just as when you say `int var1;' outside
  103. a struct it allocates storage, but not when it is inside a struct;
  104. similarly when you say `struct inner{ ...} DUMMY;' outside a struct,
  105. it allocates storage, not inside. Remember that storage is allocated
  106. only when you define a variable, not when you declare a type.
  107.    
  108. Cheers
  109. Tanmoy
  110. --
  111. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  112. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  113. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  114. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  115. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  116. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  117.